/**
 * @author Administrator
 */
var Dialog = new Class({
	Implements: [Options, Events],
	
	options: {
		className: 'dialog',
		fixed: true,
		draggable: true,
		autoPositionOnShow: true,
		isModal: true,
		maskColor: '#fff',
		maskOpacity: 0.3
	},
	
	initialize: function(options)
	{
		this.setOptions((options ||
		{}));
		
		this.Container = new Element('div', {
			'class': this.options.className,
			'styles': {
				'visibility': 'hidden',
				'position': 'absolute',
				'left': '0px',
				'top': '0px'
			}
		});
		this.Pinner = new Pin(this.Container, {
			draggable: this.options.draggable
		});
		
		this.Mask = new Element('div', {
			'styles': {
				'backgroundColor': this.options.maskColor,
				'left': '0px',
				'top': '0px'
			}
		});
		
		this.Mask.set('opacity', this.options.maskOpacity);
		this.Mask.setStyle('visibility', 'hidden');
		
		if (Browser.Engine.trident4) 
		{
			var wSize = window.getSize();
			var sSize = window.getScrollSize();
			this.Mask.setStyles({
				'position': 'absolute',
				'width': (sSize.x > wSize.x ? sSize.x : wSize.x) + 'px',
				'height': (sSize.y > wSize.y ? sSize.y : wSize.y) + 'px'
			});
		}
		else 
		{
			this.Mask.setStyles({
				'position': 'fixed',
				'width': '100%',
				'height': '100%'
			});
		}
		
		this.Showed = false;
		
		window.addEvent('domready', (function()
		{
			document.body.appendChild(this.Container);
			document.body.appendChild(this.Mask);
			this.Mask.setStyle('z-index', parseInt(this.Container.getStyle('z-index')) - 1);
		}).bind(this))
	},
	
	moveTo: function(x, y)
	{
		this.Container.setStyles({
			'left': x + 'px',
			'top': y + 'px'
		});
		if (this.options.fixed && Browser.Engine.trident4) 
		{
			var scroll = window.getScroll();
			this.Pinner.PinX = x - scroll.x;
			this.Pinner.PinY = y - scroll.y;
			
		}
	},
	
	autoPosition: function()
	{
		var mySize = this.Container.getSize();
		var scroll = window.getScroll();
		var windowSize = window.getSize();
		this.Container.setStyles({
			'left': (windowSize.x - mySize.x) / 2 + scroll.x + 'px',
			'top': (windowSize.y - mySize.y) / 2 + scroll.y + 'px'
		});
	},
	
	show: function()
	{
		if (this.options.autoPositionOnShow) 
		{
			this.autoPosition();
		}
		
		if (this.options.fixed) 
		{
			this.Pinner.pin();
		}
		
		this.Container.style.visibility = 'visible';
		
		if (this.options.isModal) 
		{
			this.Mask.style.visibility = 'visible';
		}
		
		this.Showed = true;
		this.fireEvent('show');
	},
	
	hide: function()
	{
		this.Container.style.visibility = 'hidden';
		this.Mask.style.visibility = 'hidden';
		this.Showed = false;
		this.Pinner.unpin();
		this.fireEvent('hide');
	}
});
